/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.ForgetPwdForm;
import io.renren.modules.app.form.RegisterForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.generator.entity.TMemberEntity;
import io.renren.modules.generator.service.TMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 注册
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP注册接口")
public class AppRegisterController {
    @Autowired
    private UserService userService;

    @Autowired
    private TMemberService memberService;

    @PostMapping("register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form, HttpServletRequest request){
        //表单校验
        ValidatorUtils.validateEntity(form);


        JSONObject json = (JSONObject)request.getSession().getAttribute("verifyCode");
        if(json == null){
            return R.error("verify code error");


        }
        if(!json.getString("email").equals(form.getEmail())){
            return R.error("email error");
        }
        if(!json.getString("verifyCode").equals(form.getCode())){
            return R.error("verify code error");

        }
        if((System.currentTimeMillis() - json.getLong("createTime")) > 1000 * 60 * 15){
            return R.error("verify code expired ");

        }


        TMemberEntity memberEntity= new TMemberEntity();
        memberEntity.setCreatetime(new Date());
        memberEntity.setName(form.getName());
        memberEntity.setGender(form.getGender());
        memberEntity.setEmail(form.getEmail());
        memberEntity.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        memberEntity.setModifytime(new Date());
        memberEntity.setLevel(100);
        memberService.save(memberEntity);
        memberEntity.setFamilyid(memberEntity.getUserid());



        return R.ok();
    }


    @PostMapping("forgetPassword")
    @ApiOperation("forgetPassword")
    public R forgetPassword(@RequestBody ForgetPwdForm form, HttpServletRequest request){
        //表单校验
        ValidatorUtils.validateEntity(form);


        JSONObject json = (JSONObject)request.getSession().getAttribute("verifyCode");
        if(json == null){
            return R.error("verify code error");


        }
        if(!json.getString("email").equals(form.getEmail())){
            return R.error("email error");
        }
        if(!json.getString("verifyCode").equals(form.getCode())){
            return R.error("verify code error");

        }
        if((System.currentTimeMillis() - json.getLong("createTime")) > 1000 * 60 * 15){
            return R.error("verify code expired ");

        }

        TMemberEntity memberEntity=memberService.getOne(new QueryWrapper<TMemberEntity>().eq("email",form.getEmail()));
        if (memberEntity!=null) {
            memberEntity.setPassword(DigestUtils.sha256Hex(form.getPassword()));
            memberEntity.setModifytime(new Date());
            memberService.updateById(memberEntity);
        }else{
            return R.error("user does not exist");
        }





        return R.ok();
    }
}
